CMAKE_MINIMUM_REQUIRED(VERSION 3.10)

SET(CMAKE_SYSTEM_NAME Generic)
SET(CMAKE_SYSTEM_PROCESSOR cortex-m7)
#SET(CMAKE_VERBOSE_MAKEFILE ON)

SET(CMAKE_EXPORT_COMPILE_COMMANDS ON)

SET(CMAKE_C_COMPILER "E:/Share/env-windows/tools/bin/../../tools/gnu_gcc/arm_gcc/mingw/bin/arm-none-eabi-gcc.exe")
SET(CMAKE_ASM_COMPILER "E:/Share/env-windows/tools/bin/../../tools/gnu_gcc/arm_gcc/mingw/bin/arm-none-eabi-gcc.exe")
SET(CMAKE_C_FLAGS " -mcpu=cortex-m7 -mthumb -mfpu=fpv5-d16 -mfloat-abi=hard -ffunction-sections -fdata-sections -Dgcc -O0 -gdwarf-2 -g -std=c99")
SET(CMAKE_ASM_FLAGS " -c -mcpu=cortex-m7 -mthumb -mfpu=fpv5-d16 -mfloat-abi=hard -ffunction-sections -fdata-sections -x assembler-with-cpp -Wa,-mimplicit-it=thumb  -gdwarf-2")
SET(CMAKE_C_COMPILER_WORKS TRUE)

SET(CMAKE_CXX_COMPILER "E:/Share/env-windows/tools/bin/../../tools/gnu_gcc/arm_gcc/mingw/bin/arm-none-eabi-g++.exe")
SET(CMAKE_CXX_FLAGS " -mcpu=cortex-m7 -mthumb -mfpu=fpv5-d16 -mfloat-abi=hard -ffunction-sections -fdata-sections -Dgcc -O0 -gdwarf-2 -g")
SET(CMAKE_CXX_COMPILER_WORKS TRUE)

SET(CMAKE_OBJCOPY "E:/Share/env-windows/tools/bin/../../tools/gnu_gcc/arm_gcc/mingw/bin/arm-none-eabi-objcopy.exe")
SET(CMAKE_SIZE "E:/Share/env-windows/tools/bin/../../tools/gnu_gcc/arm_gcc/mingw/bin/arm-none-eabi-size.exe")

SET(CMAKE_EXE_LINKER_FLAGS " -mcpu=cortex-m7 -mthumb -mfpu=fpv5-d16 -mfloat-abi=hard -ffunction-sections -fdata-sections -Wl,--gc-sections,-Map=rtthread.map,-cref,-u,Reset_Handler -T ${CMAKE_SOURCE_DIR}/board/linker_scripts/link.lds -Wl,--gc-sections,--print-memory-usage")

SET(CMAKE_C_STANDARD 99)
SET(CMAKE_CXX_STANDARD 17)

PROJECT(rtthread C CXX ASM)

INCLUDE_DIRECTORIES(
	.
	applications
	board
	board/CubeMX_Config/Inc
	board/port
	board/cherryusb_dev
	board/drv_test
	packages/SEGGER_RTT-latest
	packages/SEGGER_RTT-latest/RTT
	rt-thread/include
	rt-thread/libcpu/arm/common
	rt-thread/libcpu/arm/cortex-m7
	rt-thread/components/dfs/dfs_v1/include
	rt-thread/components/dfs/dfs_v1/filesystems/devfs
	rt-thread/components/dfs/dfs_v1/filesystems/elmfat
	rt-thread/components/dfs/dfs_v1/filesystems/romfs
	rt-thread/components/drivers/include
	rt-thread/components/drivers/phy
	rt-thread/components/drivers/sdio/sdhci/include
	rt-thread/components/drivers/smp_call
	rt-thread/components/drivers/spi
	rt-thread/components/drivers/spi/sfud/inc
	rt-thread/components/drivers/usb/cherryusb
	rt-thread/components/drivers/usb/cherryusb/common
	rt-thread/components/drivers/usb/cherryusb/core
	rt-thread/components/drivers/usb/cherryusb/class/hub
	rt-thread/components/drivers/usb/cherryusb/class/cdc
	rt-thread/components/drivers/usb/cherryusb/class/msc
	rt-thread/components/drivers/usb/cherryusb/class/hid
	rt-thread/components/drivers/usb/cherryusb/class/audio
	rt-thread/components/drivers/usb/cherryusb/class/video
	rt-thread/components/drivers/usb/cherryusb/class/wireless
	rt-thread/components/drivers/usb/cherryusb/class/midi
	rt-thread/components/drivers/usb/cherryusb/class/adb
	rt-thread/components/drivers/usb/cherryusb/class/dfu
	rt-thread/components/drivers/usb/cherryusb/class/vendor/net
	rt-thread/components/drivers/usb/cherryusb/class/vendor/serial
	rt-thread/components/drivers/usb/cherryusb/class/vendor/wifi
	rt-thread/components/drivers/usb/cherryusb/osal
	rt-thread/components/finsh
	rt-thread/components/libc/compilers/common/include
	rt-thread/components/libc/compilers/newlib
	rt-thread/components/libc/posix/io/epoll
	rt-thread/components/libc/posix/io/eventfd
	rt-thread/components/libc/posix/io/poll
	rt-thread/components/libc/posix/ipc
	libraries/STM32H7xx_HAL/STM32H7xx_HAL_Driver/Inc
	libraries/STM32H7xx_HAL/CMSIS/Device/ST/STM32H7xx/Include
	libraries/STM32H7xx_HAL/STM32H7xx_HAL_Driver/Inc/Legacy
	libraries/HAL_Drivers/drivers
	libraries/HAL_Drivers/drivers/config
	libraries/HAL_Drivers
	libraries/HAL_Drivers/CMSIS/Include
)

ADD_DEFINITIONS(
	-DSTM32H7B3xx
	-D__RTTHREAD__
	-DRT_USING_NEWLIBC
	-DRT_USING_LIBC
	-D_POSIX_C_SOURCE=1
	-DUSE_HAL_DRIVER
)

# Library source files
SET(RT_APPLICATIONS_SOURCES
	applications/run_in_qspi.c
	applications/run_in_sram.c
	applications/main.c
)

SET(RT_CHERRYUSB_SOURCES
	rt-thread/components/drivers/usb/cherryusb/core/usbd_core.c
	rt-thread/components/drivers/usb/cherryusb/class/hid/usbd_hid.c
	rt-thread/components/drivers/usb/cherryusb/platform/rtthread/usb_check.c
	rt-thread/components/drivers/usb/cherryusb/class/cdc/usbd_cdc_acm.c
	rt-thread/components/drivers/usb/cherryusb/port/dwc2/usb_glue_st.c
	rt-thread/components/drivers/usb/cherryusb/platform/rtthread/usb_msh.c
	rt-thread/components/drivers/usb/cherryusb/osal/usb_osal_rtthread.c
	rt-thread/components/drivers/usb/cherryusb/port/dwc2/usb_dc_dwc2.c
	rt-thread/components/drivers/usb/cherryusb/class/msc/usbd_msc.c
)

SET(RT_COMPILER_SOURCES
	rt-thread/components/libc/compilers/common/cctype.c
	rt-thread/components/libc/compilers/common/cstdlib.c
	rt-thread/components/libc/compilers/common/cstring.c
	rt-thread/components/libc/compilers/common/ctime.c
	rt-thread/components/libc/compilers/common/cunistd.c
	rt-thread/components/libc/compilers/common/cwchar.c
	rt-thread/components/libc/compilers/newlib/syscalls.c
)

SET(RT_DEVICEDRIVERS_SOURCES
	rt-thread/components/drivers/block/blk.c
	rt-thread/components/drivers/block/blk_dev.c
	rt-thread/components/drivers/block/blk_dfs.c
	rt-thread/components/drivers/block/blk_partition.c
	rt-thread/components/drivers/block/partitions/dfs.c
	rt-thread/components/drivers/block/partitions/efi.c
	rt-thread/components/drivers/core/device.c
	rt-thread/components/drivers/ipc/completion_comm.c
	rt-thread/components/drivers/ipc/completion_up.c
	rt-thread/components/drivers/ipc/condvar.c
	rt-thread/components/drivers/ipc/dataqueue.c
	rt-thread/components/drivers/ipc/pipe.c
	rt-thread/components/drivers/ipc/ringblk_buf.c
	rt-thread/components/drivers/ipc/ringbuffer.c
	rt-thread/components/drivers/ipc/waitqueue.c
	rt-thread/components/drivers/ipc/workqueue.c
	rt-thread/components/drivers/pin/dev_pin.c
	rt-thread/components/drivers/sdio/dev_block.c
	rt-thread/components/drivers/sdio/dev_mmc.c
	rt-thread/components/drivers/sdio/dev_mmcsd_core.c
	rt-thread/components/drivers/sdio/dev_sd.c
	rt-thread/components/drivers/sdio/dev_sdio.c
	rt-thread/components/drivers/serial/dev_serial.c
	rt-thread/components/drivers/spi/dev_qspi_core.c
	rt-thread/components/drivers/spi/dev_spi.c
	rt-thread/components/drivers/spi/dev_spi_core.c
	rt-thread/components/drivers/spi/dev_spi_flash_sfud.c
	rt-thread/components/drivers/spi/sfud/src/sfud.c
	rt-thread/components/drivers/spi/sfud/src/sfud_sfdp.c
)

SET(RT_DRIVERS_SOURCES
	board/CubeMX_Config/Src/stm32h7xx_hal_msp.c
	board/board.c
	board/cherryusb_dev/my_msc_blkdev.c
	board/cherryusb_dev/test.c
	board/cherryusb_dev/usbd_init.c
	board/drv_test/octospi.c
	board/drv_test/w25q128jv.c
	board/port/drv_lcd_cdev.c
	board/port/drv_mpu.c
	board/port/port_mount_table.c
	board/port/port_ospi_flash.c
	board/port/port_spi_flash.c
	libraries/STM32H7xx_HAL/CMSIS/Device/ST/STM32H7xx/Source/Templates/gcc/startup_stm32h7b3xx.s
	libraries/HAL_Drivers/drivers/drv_gpio.c
	libraries/HAL_Drivers/drivers/drv_ospi.c
	libraries/HAL_Drivers/drivers/drv_qspi.c
	libraries/HAL_Drivers/drivers/drv_spi.c
	libraries/HAL_Drivers/drivers/drv_usart.c
	libraries/HAL_Drivers/drv_common.c
)

SET(RT_FILESYSTEM_SOURCES
	rt-thread/components/dfs/dfs_v1/filesystems/devfs/devfs.c
	rt-thread/components/dfs/dfs_v1/filesystems/elmfat/dfs_elm.c
	rt-thread/components/dfs/dfs_v1/filesystems/elmfat/ff.c
	rt-thread/components/dfs/dfs_v1/filesystems/elmfat/ffunicode.c
	rt-thread/components/dfs/dfs_v1/filesystems/romfs/dfs_romfs.c
	rt-thread/components/dfs/dfs_v1/src/dfs.c
	rt-thread/components/dfs/dfs_v1/src/dfs_file.c
	rt-thread/components/dfs/dfs_v1/src/dfs_fs.c
	rt-thread/components/dfs/dfs_v1/src/dfs_posix.c
)

SET(RT_FINSH_SOURCES
	rt-thread/components/finsh/cmd.c
	rt-thread/components/finsh/msh_parse.c
	rt-thread/components/finsh/shell.c
	rt-thread/components/finsh/msh.c
	rt-thread/components/finsh/msh_file.c
)

SET(RT_KERNEL_SOURCES
	rt-thread/src/clock.c
	rt-thread/src/components.c
	rt-thread/src/cpu_up.c
	rt-thread/src/defunct.c
	rt-thread/src/idle.c
	rt-thread/src/ipc.c
	rt-thread/src/irq.c
	rt-thread/src/kservice.c
	rt-thread/src/mem.c
	rt-thread/src/mempool.c
	rt-thread/src/object.c
	rt-thread/src/scheduler_comm.c
	rt-thread/src/scheduler_up.c
	rt-thread/src/thread.c
	rt-thread/src/timer.c
)

SET(RT_KLIBC_SOURCES
	rt-thread/src/klibc/kstring.c
	rt-thread/src/klibc/rt_vsnprintf_tiny.c
	rt-thread/src/klibc/kstdio.c
	rt-thread/src/klibc/kerrno.c
	rt-thread/src/klibc/rt_vsscanf.c
)

SET(RT_LIBCPU_SOURCES
	rt-thread/libcpu/arm/common/atomic_arm.c
	rt-thread/libcpu/arm/common/div0.c
	rt-thread/libcpu/arm/common/showmem.c
	rt-thread/libcpu/arm/cortex-m7/context_gcc.S
	rt-thread/libcpu/arm/cortex-m7/cpu_cache.c
	rt-thread/libcpu/arm/cortex-m7/cpuport.c
)

SET(RT_LIBRARIES_SOURCES
	libraries/STM32H7xx_HAL/STM32H7xx_HAL_Driver/Src/stm32h7xx_ll_sdmmc.c
	libraries/STM32H7xx_HAL/CMSIS/Device/ST/STM32H7xx/Source/Templates/system_stm32h7xx.c
	libraries/STM32H7xx_HAL/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_ospi.c
	libraries/STM32H7xx_HAL/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_rng.c
	libraries/STM32H7xx_HAL/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_comp.c
	libraries/STM32H7xx_HAL/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_usart.c
	libraries/STM32H7xx_HAL/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_uart.c
	libraries/STM32H7xx_HAL/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_spi.c
	libraries/STM32H7xx_HAL/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal.c
	libraries/STM32H7xx_HAL/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_pwr_ex.c
	libraries/STM32H7xx_HAL/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_gpio.c
	libraries/STM32H7xx_HAL/STM32H7xx_HAL_Driver/Src/stm32h7xx_ll_delayblock.c
	libraries/STM32H7xx_HAL/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_crc_ex.c
	libraries/STM32H7xx_HAL/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_uart_ex.c
	libraries/STM32H7xx_HAL/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_dma.c
	libraries/STM32H7xx_HAL/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_cryp_ex.c
	libraries/STM32H7xx_HAL/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_dma_ex.c
	libraries/STM32H7xx_HAL/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_cryp.c
	libraries/STM32H7xx_HAL/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_pwr.c
	libraries/STM32H7xx_HAL/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_rcc_ex.c
	libraries/STM32H7xx_HAL/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_rcc.c
	libraries/STM32H7xx_HAL/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_crc.c
	libraries/STM32H7xx_HAL/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_sd.c
	libraries/STM32H7xx_HAL/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_cec.c
	libraries/STM32H7xx_HAL/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_sram.c
	libraries/STM32H7xx_HAL/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_cortex.c
	libraries/STM32H7xx_HAL/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_mdma.c
)

SET(RT_POSIX_SOURCES
)

SET(RT_SEGGER_RTT_SOURCES
	packages/SEGGER_RTT-latest/RTT/SEGGER_RTT_printf.c
	packages/SEGGER_RTT-latest/RTT/SEGGER_RTT.c
	packages/SEGGER_RTT-latest/adapter/drv_rtt.c
)

SET(RT_SMP_SOURCES
)

SET(RT_UTESTCASES_SOURCES
)

# Library search paths
# Library local macro definitions
SET(RT_DEVICEDRIVERS_DEFINES
	__RT_IPC_SOURCE__
)

SET(RT_KERNEL_DEFINES
	__RT_KERNEL_SOURCE__
)

# Library dependencies
SET(RT_COMPILER_LIBS
	c
	m
)

# Libraries
ADD_LIBRARY(rtt_CherryUSB OBJECT ${RT_CHERRYUSB_SOURCES})
ADD_LIBRARY(rtt_Compiler OBJECT ${RT_COMPILER_SOURCES})
ADD_LIBRARY(rtt_DeviceDrivers OBJECT ${RT_DEVICEDRIVERS_SOURCES})
ADD_LIBRARY(rtt_Drivers OBJECT ${RT_DRIVERS_SOURCES})
ADD_LIBRARY(rtt_Filesystem OBJECT ${RT_FILESYSTEM_SOURCES})
ADD_LIBRARY(rtt_Finsh OBJECT ${RT_FINSH_SOURCES})
ADD_LIBRARY(rtt_Kernel OBJECT ${RT_KERNEL_SOURCES})
ADD_LIBRARY(rtt_klibc OBJECT ${RT_KLIBC_SOURCES})
ADD_LIBRARY(rtt_libcpu OBJECT ${RT_LIBCPU_SOURCES})
ADD_LIBRARY(rtt_Libraries OBJECT ${RT_LIBRARIES_SOURCES})
ADD_LIBRARY(rtt_SEGGER_RTT OBJECT ${RT_SEGGER_RTT_SOURCES})

# Interface libraries
ADD_LIBRARY(rtt_POSIX INTERFACE)
ADD_LIBRARY(rtt_smp INTERFACE)
ADD_LIBRARY(rtt_utestcases INTERFACE)

# Private macros
TARGET_COMPILE_DEFINITIONS(rtt_DeviceDrivers PRIVATE ${RT_DEVICEDRIVERS_DEFINES})
TARGET_COMPILE_DEFINITIONS(rtt_Kernel PRIVATE ${RT_KERNEL_DEFINES})

# Interface library search paths
TARGET_LINK_LIBRARIES(rtt_Compiler INTERFACE ${RT_COMPILER_LIBS})

ADD_EXECUTABLE(${CMAKE_PROJECT_NAME}.elf ${RT_APPLICATIONS_SOURCES})
TARGET_LINK_LIBRARIES(${CMAKE_PROJECT_NAME}.elf
	rtt_CherryUSB
	rtt_Compiler
	rtt_DeviceDrivers
	rtt_Drivers
	rtt_Filesystem
	rtt_Finsh
	rtt_Kernel
	rtt_klibc
	rtt_libcpu
	rtt_Libraries
	rtt_SEGGER_RTT
	rtt_POSIX
	rtt_smp
	rtt_utestcases
)

ADD_CUSTOM_COMMAND(TARGET ${CMAKE_PROJECT_NAME}.elf POST_BUILD 
	COMMAND ${CMAKE_OBJCOPY} -O binary ${CMAKE_PROJECT_NAME}.elf rtthread.bin
	COMMAND ${CMAKE_SIZE} ${CMAKE_PROJECT_NAME}.elf
)

# if custom.cmake is exist, add it
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/custom.cmake)
include(${CMAKE_CURRENT_SOURCE_DIR}/custom.cmake)
endif()
